<template>
  <div class="user">
    <el-input
        class="input"
        placeholder="请输入昵称"
        v-model="keywords"
        clearable>
    </el-input>
    <el-select clearable style="margin: 0 20px;" v-model="enabled" placeholder="请选择账号状态">
      <el-option
              v-for="item in enabledOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
      </el-option>
    </el-select>
    <el-select clearable style="margin: 0 20px;" v-model="admin" placeholder="请选择用户类型">
      <el-option
              v-for="item in adminOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
      </el-option>
    </el-select>
    <el-button @click="searchUser" icon="el-icon-search" type="primary">搜索</el-button>
    <el-button @click="deleteUserBatch" icon="el-icon-delete" type="danger" :disabled="userIds.length === 0">批量删除</el-button>
    <el-table v-loading="loading" :data="tableData" class="table" @selection-change="changeSelection">
      <el-table-column
              type="selection"
              width="55">
      </el-table-column>
      <el-table-column
              prop="avatarUrl"
              label="头像"
              width="150">
        <template slot-scope="scope">
          <el-avatar :src="scope.row.avatarUrl"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column
              prop="nickName"
              label="昵称"
              width="150">
      </el-table-column>
      <el-table-column
              prop="gender"
              label="性别"
              width="150">
        <template slot-scope="scope">{{ scope.row.gender ? '男': '女' }}</template>
      </el-table-column>
      <el-table-column
              prop="phoneNumber"
              label="手机号"
              width="200">
      </el-table-column>
      <el-table-column
              prop="enabled"
              label="账号状态"
              width="300">
        <template slot-scope="scope">
          <el-switch
                  @change="changeEnabled(scope.row)"
                  v-model="scope.row.enabled"
                  active-text="可用"
                  inactive-text="禁用">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
              prop="admin"
              label="用户类型"
              width="300">
        <template slot-scope="scope">
          <el-switch
                  @change="changeAdmin(scope.row)"
                  v-model="scope.row.admin"
                  active-text="管理员"
                  inactive-text="普通用户">
          </el-switch></template>
      </el-table-column>
      <el-table-column
              fixed="right"
              label="操作"
              width="150">
        <template slot-scope="scope">
          <el-popconfirm class="confirm" title="确定删除？" @confirm="deleteUser(scope.row)">
            <el-button  size="small" slot="reference" type="danger" icon="el-icon-delete">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
            @current-change="changePage"
            class="page"
            background
            layout="prev, pager, next"
            :page-size="pageSize"
            :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: "User",
  data () {
    return {
      loading: false,
      tableData: [],
      keywords: '',
      enabled: '',
      admin: '',
      pageNum: 1,
      pageSize: 10,
      total: 0,
      userIds: [],
      enabledOptions: [{
        value: true,
        label: '可用'
      }, {
        value: false,
        label: '禁用'
      }],
      adminOptions: [{
        value: true,
        label: '管理员'
      }, {
        value: false,
        label: '普通用户'
      }],
    }
  },
  methods:{
    changeEnabled (row) {
      const userBo = {
        enabled: row.enabled
      }
      this.$axios.put(`/user/${row.id}`, userBo).then(res => {
        if (res.code === 200) {
          this.searchUser()
        }
      })
    },
    changeAdmin (row) {
      const userBo = {
        admin: row.admin
      }
      this.$axios.put(`/user/${row.id}`, userBo).then(res => {
        if (res.code === 200) {
          this.searchUser()
        }
      })
    },
    searchUser () {
      this.pageNum = 1
      this.loadTableData()
    },
    deleteUser (row) {
      this.$axios.delete(`/user/${row.id}`).then(res => {
        if (res.code === 200) {
          this.searchUser()
        }
      })
    },
    deleteUserBatch () {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.delete('/user/batch', {
          data: this.userIds
        }).then(res => {
          if (res.code === 200) {
            this.searchUser()
          }
        })
      })
    },
    changePage (pageNum) {
      this.pageNum = pageNum
      this.loadTableData()
    },
    changeSelection (rows) {
      this.userIds = []
      for (const row of rows) {
        this.userIds.push(row.id)
      }
    },
    loadTableData () {
      this.loading = true
      this.$axios.get(`/user?keywords=${this.keywords}&enabled=${this.enabled}&admin=${this.admin}&pageNum=${this.pageNum}&pageSize=${this.pageSize}`).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.records
          this.total = res.data.total
        }
      })
    }
  },
  mounted() {
    this.loadTableData()
  }
}
</script>
<style lang="scss" scoped>
  .user {
    width: 100%;
    height: 100%;
  }
  .input {
    width: 300px;
    margin: 0 20px 0 0;
  }
  .table {
    width: 100%;
    margin-top: 20px;
  }
  .confirm {
    margin-left: 20px;
  }
  .page {
    margin: 20px 0;
  }
</style>
